/** 打击者1945plus,主要是游聚用

  By setycyas @2025-01-18
*/

/* 全局设定
*/ {
#Warn
#MaxThreadsPerHotkey 1
SetWorkingDir(A_ScriptDir)
#Include "..\Class\FaceDetector.2.ahk"
#Include "..\Class\CommonMacro.2.ahk"
#Include "..\Class\CommonUI.2.ahk"
#Include "..\Class\MyFuns.2.ahk"
#Include "..\Class\MyEmu.2.ahk"
#Include "..\Class\OrderMap.2.ahk"
#Include "..\Class\RulerWindow.2.ahk"
;SendMode("Event")
SendMode("Input")
}

/* 制作打击者1945plus的宏类
*/ class S1945pMacro extends CommonMacro {

  pressDuration := 30
  aFlag := False ;a键按下与否的标记
  ;游聚窗口变换参数,先初始化,在相关函数中再赋值,为保证rulerWindow效果
  ;需要记录这些参数,使用默认值是为了召唤标尺时如果未计算新位置也有个初值
  xzone_x := 138
  xzone_y := -56
  xzone_width := 1051
  xzone_height := 814

  /* 函数
  */
  ;需要蓄力delay时间的蓄力攻击
  _蓄力攻击(delay){
    if (this.aFlag) {
      this._up(1)
      sleep(30)
      this.aFlag := True
    }
    this._down(1)
    this.aFlag := True
    sleep(delay)
    this._up(1)
    this.aFlag := False
  }

  /* 新增的宏函数
  */
  ;改装一下连发A,当被蓄力中断后判断一下按键的放开状态
  _连发A(key){
    local vk := GetKeyVK(MyFuns.getHotkey(key))
    while (MyFuns.getVkState(vk)) {
      this._down(1)
      this.aFlag := True
      sleep(this.pressDuration)
      if (this.aFlag) {
        this._up(1)
        this.aFlag := False
      }
      sleep(this.autoFireDelay)
    }
  }
  ;各机体的蓄力
  _一键蓄力(key){
    this._蓄力攻击(500)
    if (MyFuns.getKeyState(key)) {
      this._连发A(key)
    }
  }
  ;轻微位移用
  _右微移(key){
    MyFuns.press("right", 50)
    sleep(50)
  }
  _左微移(key){
    MyFuns.press("left", 50)
    sleep(50)
  }
  ;fbneo64的窗口变换,整个窗体长宽比大概是0.7
  _FBNEO64窗口变换(key){
    static W_TITLE := "ahk_exe fbneo64.exe"
    static HEIGHT := 768
    static BORD_SIZE := [50, 10, 10, 10]
    static RATE := 600/800
    ;执行
    MyFuns.setEmuWindow(W_TITLE, BORD_SIZE, RATE, HEIGHT, 200, 0)
  }
  ;游聚屏幕变换
  _游聚Tab屏变换(key){  
    static W_TITLE := "(setycyas) ahk_exe x-zone.exe"
    ;自定的两边留空和标题栏宽度,窗口参数
    static SIDE := 20
    static TITLE_BAR := 76
    ;窗口变换参数赋值
    this.xzone_height := (768-this.testParams[2])+(TITLE_BAR-20)
    this.xzone_width := Round((768-this.testParams[2])*(4/3))+SIDE*2
    this.xzone_x:= Round((1366-this.xzone_width)/2)-this.testParams[1]
    this.xzone_y := -(TITLE_BAR-20)
    ;执行
    MyFuns.setWindow(W_TITLE, this.xzone_width, this.xzone_height, 
      this.xzone_x, this.xzone_y)
  }
  _游聚Tab屏还原(key){  
    static W_TITLE := "(setycyas) ahk_exe x-zone.exe"
    static HEIGHT := 600
    static WIDTH := 800
    ;执行
    MyFuns.setWindow(W_TITLE, WIDTH, HEIGHT, 0, 0)
  }
  _固定开始测试(key){ ;游聚用,估计固定的delay会形成固定的关卡顺序
    ;delay=720-735,海路艇空
    ;delay=750,路海艇空
    ;delay=980-1000,路艇海空
    ;delay=1020,艇空路海
    ;delay=1060,路艇海空
    local delay := this.testParams[3]
    MyFuns.press("5", 30)
    sleep(delay)
    MyFuns.press("1", 30)
  }

  /* 重载宏接口
  */
  _setMacro(){
    static obm := ObjBindMethod ;函数名简写,制作对象绑定函数
    ;制作宏相关Map
    this.macroMap := OrderMap()
    this.macroMap["连发A"] := obm(this, "_连发A")
    this.macroMap["一键蓄力"] := obm(this, "_一键蓄力")
    this.macroMap["左微移"] := obm(this, "_左微移")
    this.macroMap["右微移"] := obm(this, "_右微移")
    this.macroMap["FBNEO64窗口变换"] := obm(this, "_FBNEO64窗口变换")
    this.macroMap["游聚Tab屏变换"] := obm(this, "_游聚Tab屏变换")
    this.macroMap["游聚Tab屏还原"] := obm(this, "_游聚Tab屏还原")
    this.macroMap["固定开始测试"] := obm(this, "_固定开始测试")
    this.macroMap["空函数"] := obm(this, "_空函数")
    ;制作指定宏顺序的数组
    this.macroArr := this.macroMap.getKeys()
  }
}

/* 制作打击者1945plus专用UI
*/
class S1945pUI extends CommonUI {

  HOTKEY_NUM := 7
  EXE_ARRAY := [MyEmu.XZONE, MyEmu.MAME, MyEmu.FBNEO64]

  /* 构造函数
  */
  __New(macro){
    super.__New("打击者1945plus", macro)
    this._preset := OrderMap()
    this._macro.UI := this ;绑定UI
    ;设置preset和提升蓄力宏的优先级
    this._preset["非游聚"] := ["连发A", "一键蓄力", "左微移", 
      "右微移", "FBNEO64窗口变换", "空函数", "空函数"]
    this._preset["游聚"] := ["连发A", "一键蓄力", "左微移", 
      "右微移", "游聚Tab屏变换", "游聚Tab屏还原", "固定开始测试"]
    this.MACRO_PRIORITY["一键蓄力"] := "p1"
    
  }

  /* 重写接口
  */

  /* 重写setup,更改可选的exe,读入预设
  */
  setup(){
    super.setup()
    this._myGui.opt("+AlwaysOnTop")
    this._myGui.show("x1100 y230")
    this.readPresetMenu("游聚")
  }
  
  ;自定义gui组件标签页,增加一个挡板召唤键
  _customGui(){
    super._customGui()
    ;增加召唤标尺窗口按键
    this._rulerButton := this._myGui.Add("Button", "Default w200", "召唤标尺")
    this._rulerButton.OnEvent(
      "Click", 
      ObjBindMethod(this, "_onRulerButtonClick")
    )
  }

  /* 自定义函数
  */
  ;召唤标尺窗口
  _onRulerButtonClick(btnObj, info){
    static ruler := ""
    static wSize := [100, 20]
    static infos := [
      [0, "FFFFFF", "123456"]
    ]
    static x := Round(this._macro.xzone_x+this._macro.xzone_width/2)
    static isActive := False
    
    if ruler {
      ;ruler._gui.Restore()
      if isActive {
        ruler._gui.Hide()
        isActive := False
      } else {
        ruler._gui.Restore()
        isActive := True
      }
    } else {
      ruler := RulerWindow(wSize, infos)
      ruler._gui.opt("+AlwaysOnTop")
      ruler.show("x" . x . " y758 ")
      isActive := True
    }
  }

}

/* 运行当前脚本
*/ if (A_ScriptName == "打击者1945plus.2.ahk") {
  iconFile := "..\Icon\战国之刃小夜.png"
  TraySetIcon(iconFile, , 1)

  face := FaceDetector() ;方向检测对象
  macro := S1945pMacro(face, ["a","space"]) ;宏对象,有界面时第二个参数可以随便写但长度不要小于2
  ;新建界面对象并显示
  ui := S1945pUI(macro)
  ui.setup()
  ;更新组件内容
  ui._exeComboBox.Choose(MyEmu.XZONE)
  ;ui._exeComboBox.Choose(MyEmu.FBNEO64)
  ui._gameKeyEdit.Text := "a,space"
  ui._hotkeyEdit.Text := "~s,~d,~e,~f,~o,~l,~i"
  ui._fpsEdit.Text := 10
  ui._testEdit.Text := "20,10,1020,30"
  ui.readPresetMenu("游聚")
}
